<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.rtcoperator_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getPage, getDetail, add, update, remove} from "@/api/bureau/userofficer.js";
  import {mapGetters} from "vuex";
  import CommonFilters from "@/mixins/CommonFilters";
  export default {
    name: "app",
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: false,
          selection: true,
          column: [
            {
              label: "排序",
              prop: "orderNo",
              type: "number"
            },
            {
              label: "名称",
              prop: "name",
              rules: [{
                required: true,
                message: "请输入名称",
                trigger: "blur"
              }]
            },
            {
              label: "标识",
              prop: "delFlag",
              disabled: true
            },
            {
              label: "URL",
              prop: "url",
              rules: [{
                required: true,
                message: "请输入URL",
                trigger: "blur"
              }]
            },
            {
              label: "首页",
              prop: "indexUrl",
              rules: [{
                required: true,
                message: "请输入首页",
                trigger: "blur"
              }]
            },
          ]
        },
        data: [],
        accidentUserId: null,
      }
    },
    mixins: [CommonFilters],
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.rtcoperator_add, false),
          viewBtn: this.vaildData(this.permission.rtcoperator_view, false),
          delBtn: this.vaildData(this.permission.rtcoperator_delete, false),
          editBtn: this.vaildData(this.permission.rtcoperator_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    mounted() {

    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getPage(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          this.page.total = res.data.count;
          // this.data = res.data.data;
          this.data = [
            {
              "createTime": "2021-05-19 15:04:55",
              "creator": null,
              "updateTime": null,
              "updator": null,
              "delFlag": 1,
              "keyword": null,
              "id": "1376919254872035330",
              "name": "门户",
              "sn": "portal",
              "secretKey": "K9909Jszc%YcleRr50c9aRO88q5QdxK#~0XeJz8FU89092MA5ytwyE%bFTL0MUwF",
              "status": 1,
              "url": "http://localhost:8000",
              "indexUrl": "/index",
              "image": "ant-design:account-book-filled",
              "note": null,
              "orderNo": 1
            },
            {
              "createTime": "2021-05-19 15:05:02",
              "creator": null,
              "updateTime": null,
              "updator": null,
              "delFlag": 1,
              "keyword": null,
              "id": "1376919486070460417",
              "name": "流程中心",
              "sn": "flow",
              "secretKey": "Fu9E9N9m19b52@H9A6YWa0t8T2&$1Y~lt0d2nO9du1SP9m71X7bk@0551hVGG!K7",
              "status": 1,
              "url": "http://localhost:8989",
              "indexUrl": "/index.html",
              "image": null,
              "note": null,
              "orderNo": 1
            },
            {
              "createTime": "2021-05-19 15:05:10",
              "creator": null,
              "updateTime": null,
              "updator": null,
              "delFlag": 1,
              "keyword": null,
              "id": "1376919513455071234",
              "name": "MDM",
              "sn": "mdm",
              "secretKey": "g71U1Q7$cxxmv1R3PcC^99ypS0@9ne37ol1U1U30a719Ql@33m2cG1V950h8w0Nu",
              "status": 1,
              "url": "http://localhost:8889",
              "indexUrl": "/index.html",
              "image": null,
              "note": null,
              "orderNo": 11
            },
            {
              "createTime": "2021-05-19 15:05:13",
              "creator": null,
              "updateTime": null,
              "updator": null,
              "delFlag": 1,
              "keyword": null,
              "id": "1376919546057396226",
              "name": "权限系统",
              "sn": "privilege",
              "secretKey": "a5q27p0E12ZjJm8Vg9J*1gs21f9lt0kP7L09v49s~Kk7ufcG4oOr#959lbfhj78c",
              "status": 1,
              "url": "http://localhost:9988",
              "indexUrl": "/index.html",
              "image": null,
              "note": null,
              "orderNo": 33
            }
          ];
          this.selectionClear();
        });
        this.loading = false;
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
